package com.serviceprovider.business.module.admin.action.controller;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.acl.model.ActionApplyForm;
import com.serviceprovider.business.module.admin.acl.services.dao.interfaces.IAclClassService;
import com.serviceprovider.business.module.admin.action.model.AclAction;
import com.serviceprovider.business.module.admin.action.services.dao.interfaces.IAclActionService;
import com.serviceprovider.business.module.admin.action.services.dao.interfaces.IActionApplyService;
import com.serviceprovider.business.module.admin.role.services.dao.interfaces.IRoleService;
import com.serviceprovider.business.utility.ApplicationConfigProperties;
import com.serviceprovider.business.utility.RequestConstants;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

//~--- JDK imports ------------------------------------------------------------

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class ActionController {
    @Autowired
    IAclActionService           aclActionService;
    @Autowired
    IAclClassService            aclClassService;
    @Autowired
    IActionApplyService         actionApplyServiceImpl;
    @Autowired
    ApplicationConfigProperties applicationConfigProperties;
    @Autowired
    IRoleService                roleService;

    @RequestMapping(
        value  = RequestConstants.ActionRequests.CREATE_ACTION,
        method = RequestMethod.GET
    )
    public ModelAndView enterCreateRole(HttpSession sess) {
        ModelAndView mav = new ModelAndView(RequestConstants.ActionRequests.VIEW_CREATE_ACTION);

        mav.addObject("aclAction", new AclAction());

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.ActionRequests.CREATE_ACTION,
        method = RequestMethod.POST
    )
    public ModelAndView createRole(@ModelAttribute("aclAction") AclAction form) {
        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.ActionRequests.VIEW_ACTIONS);

        aclActionService.insertAclAction(form);

        return mav;
    }

    @RequestMapping(value = RequestConstants.ActionRequests.VIEW_ACTIONS)
    public ModelAndView view(HttpServletRequest request, HttpSession sess) {
        ModelAndView mav        = new ModelAndView(RequestConstants.ActionRequests.VIEW_VIEW_ACTIONS);
        int          recordSize = applicationConfigProperties.getRecordSize("view_actions.records.size");
        int          pageNo     = 1;

        try {
            pageNo = Integer.parseInt(request.getParameter("pageNo"));
        } catch (NumberFormatException ex) {}

        String sortOrder = ((request.getParameter("sortOrder") == null)
                            || (request.getParameter("sortOrder").length() == 0))
                           ? "asc"
                           : request.getParameter("sortOrder");
        String sortColumn = ((request.getParameter("sortColumn") == null)
                             || (request.getParameter("sortColumn").length() == 0))
                            ? "id"
                            : request.getParameter("sortColumn");

        mav.addObject("aclActionList", aclActionService.findAll(pageNo, recordSize, sortColumn, sortOrder));

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.ActionRequests.MANAGE_ACTIONS,
        method = RequestMethod.GET
    )
    public ModelAndView enterActionApplications() {
        ModelAndView mav = new ModelAndView(RequestConstants.ActionRequests.VIEW_MANAGE_ACTIONS);

        mav.addObject("aclClassList", aclClassService.findAll());
        mav.addObject("aclActionList", aclActionService.findAll());
        mav.addObject("roleList", roleService.findAll());
        mav.addObject("aclActionApplyList", actionApplyServiceImpl.findAll());

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.ActionRequests.MANAGE_ACTIONS,
        method = RequestMethod.POST
    )
    public ModelAndView saveActionApplications(@ModelAttribute("actionApplyForm") ActionApplyForm form) {
        ModelAndView mav = new ModelAndView(RequestConstants.ActionRequests.VIEW_MANAGE_ACTIONS);

        // if(true){throw new RuntimeException("x = "+form.getAclActionApplyList().size());}
        actionApplyServiceImpl.save(form.getAclActionApplyList());
        mav.addObject("aclClassList", aclClassService.findAll());
        mav.addObject("aclActionList", aclActionService.findAll());
        mav.addObject("roleList", roleService.findAll());
        mav.addObject("aclActionApplyList", actionApplyServiceImpl.findAll());

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.ActionRequests.EDIT_ACTION,
        method = RequestMethod.GET
    )
    public ModelAndView enterEdit(HttpServletRequest req, HttpSession sess) {
        ModelAndView mav = new ModelAndView(RequestConstants.ActionRequests.VIEW_EDIT_ACTION);

        mav.addObject("aclAction", aclActionService.getAclActionById(Integer.parseInt(req.getParameter("id"))));

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.ActionRequests.EDIT_ACTION,
        method = RequestMethod.POST
    )
    public ModelAndView edit(@ModelAttribute("aclAction") AclAction form) {
        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.ActionRequests.VIEW_ACTIONS);

        aclActionService.updateAclAction(form);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.ActionRequests.DELETE_ACTIONS,
        method = RequestMethod.POST
    )
    public ModelAndView deleteRoles(HttpServletRequest req) {
        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.ActionRequests.VIEW_ACTIONS);

        aclActionService.deleteAclActionsById(toInt(req.getParameterValues("ids")));

        return mav;
    }

    public static int[] toInt(String[] arr) {
        int[] ret = new int[arr.length];

        for (int i = 0; i < arr.length; i++) {
            ret[i] = Integer.parseInt(arr[i]);
        }

        return ret;
    }
}



